//
//  ViewController.swift
//  SwiftTest
//
//  Created by Linyongzhi on 16/4/22.
//  Copyright © 2016年 Uama. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var logoImageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func onSwipe(sender: AnyObject) {
        
    }
    
    private func addGestureRecognizer() {
        let gestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.onSwipe(_:)))
        gestureRecognizer.direction = [.Left, .Right]
        self.view.addGestureRecognizer(gestureRecognizer)
        
        let gestureRecognizer2 = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.onSwipe(_:)))
        gestureRecognizer2.direction = [.Up, .Down]
        self.view.addGestureRecognizer(gestureRecognizer2)
    }
    
    @IBAction func onButtonTapped(sender: AnyObject) {
        
//        let animation = self.createBasicAnimation();
        let animation = self.createKeyFrameAnimation()
//        let pathAnimation = self.createPathAnimation()
//        let animation = self.createBasicAnimationWithTiming()
//        let animation = self.createAnimationGroup()
        
        self.logoImageView.layer.addAnimation(animation, forKey: "basic");
        
//        var position = self.logoImageView.layer.position
//        position.x = 300
//        
//        self.logoImageView.layer.position = position
        if #available(iOS 8.0, *) {
            let VC = YZTableViewController(nibName: "YZTableViewController", bundle: nil)
            self.navigationController?.pushViewController(VC, animated: true)
        } else {
            // Fallback on earlier versions
        }
        
        
    }
    
    private func createAnimationGroup() -> CAAnimationGroup {
        let animationSize = self.createBasicAnimation()
        let animationPosition = self.createBasicAnimationWithTiming()
        
        let animationGroup = CAAnimationGroup()
        animationGroup.animations = [animationSize, animationPosition]
        animationGroup.duration = 2
//        animationGroup.beginTime = 0.5
        
        return animationGroup
    }
    
    private func createBasicAnimationWithTiming() -> CABasicAnimation {
        let animation = CABasicAnimation()
        
        animation.fromValue = 0
        animation.toValue = 300
        animation.keyPath = "position.x"
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault)
        animation.duration = 1
        
        return animation
    }
    
    private func createBasicAnimation() -> CABasicAnimation {
        let animation = CABasicAnimation()
        
        let fromSize = NSValue(CGSize: CGSizeMake(0, 0))
        let toSize = NSValue(CGSize: CGSizeMake(29, 29))
        
        animation.keyPath = "bounds.size"
        animation.fromValue = fromSize;
        animation.toValue = toSize
        animation.duration = 1
        
        return animation
    }
    
    private func createKeyFrameAnimation() -> CAKeyframeAnimation {
        let currentX = 0
        
        let animation = CAKeyframeAnimation()
//        animation.keyTimes = [0, 1/6.0, 3/6.0, 5/6.0, 1]
        animation.values = [currentX, currentX + 10, currentX - 10, currentX + 10, currentX]
        animation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut),
        CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut),
        CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut),
        CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)]
        animation.keyPath = "position.x"
        animation.duration = 0.7
        
        animation.additive = true
        
        return animation
    }
    
    private func createPathAnimation() -> CAKeyframeAnimation {
        let rect = CGRectMake(-150, -150, 300, 300)
        
        let animation = CAKeyframeAnimation()
        
        animation.keyPath = "position"
        animation.path = CGPathCreateWithEllipseInRect(rect, nil)
        animation.duration = 4
        animation.additive = true
        animation.repeatCount = 2
        animation.calculationMode = kCAAnimationPaced
        animation.rotationMode = nil//kCAAnimationRotateAuto
        
        return animation
    }
}

